package com.jdy.haoduoai.adapter;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.ColorMatrixColorFilter;
import android.util.DisplayMetrics;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.jdy.haoduoai.activity.BaseActivity;
import com.jdy.haoduoai.activity.WallPicActivity;
import com.jdy.haoduoai.adapter.MyTaskAdapter.OnCheckListener;
import com.jdy.haoduoai.model.ImageItem;
import com.jdy.haoduoai.model.PhotoInfoAlbum;
import com.jdy.haoduoai.util.Tools;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.ycsj.goldmedalnewconcept.R;

import java.util.ArrayList;

//import com.jdy.haoduoai.R;

@SuppressLint("InflateParams")
public class BabyPhotoCloudAdapter extends BaseAdapter {
  private BaseActivity mContext;
  private ArrayList<PhotoInfoAlbum> mList;
  private LayoutInflater mInflater;
  @SuppressWarnings("unused")
  private boolean isEditor = false, isEditMode;
  public DisplayImageOptions options;
  private ButtonClickListener mButtonClickListener;
  private final SparseArray<View> saArray = new SparseArray<View>();

  public final float[] BT_SELECTED = new float[] { 1, 0, 0, 0, -50, 0, 1, 0, 0,
      -50, 0, 0, 1, 0, -50, 0, 0, 0, 1, 0 };

  public void setEditMode(boolean isEditMode) {
    this.isEditMode = isEditMode;
    notifyDataSetChanged();
  }

  @SuppressWarnings("unused")
  private boolean isSelectAll = false;

  private OnCheckListener mOnCheckListener;

  public OnCheckListener getOnCheckListener() {
    return mOnCheckListener;
  }

  public void setOnCheckListener(OnCheckListener mOnCheckListener) {
    this.mOnCheckListener = mOnCheckListener;
  }

  // public interface OnCheckListener {
  // void onCheckClick();
  // }

  public void setData(ArrayList<PhotoInfoAlbum> list) {
    mList = list;
  }

  public void updataView(int posi, ListView listView, String txt) {
    int visibleFirstPosi = listView.getFirstVisiblePosition();
    int visibleLastPosi = listView.getLastVisiblePosition();
    if (posi >= visibleFirstPosi && posi <= visibleLastPosi) {
      View view = listView.getChildAt(posi - visibleFirstPosi);
      ViewHolder holder = (ViewHolder) view.getTag();
      if (holder != null) {
        holder.tv_content.setText(txt);
      }

      mList.get(posi).title = txt;
    } else {
      mList.get(posi).title = txt;
    }
  }

  DisplayMetrics dm;
  int screenW, screenH;

  public BabyPhotoCloudAdapter(BaseActivity context,
      ArrayList<PhotoInfoAlbum> list) {
    this.mContext = context;
    this.mList = list;

    dm = new DisplayMetrics();
    mContext.getWindowManager().getDefaultDisplay().getMetrics(dm); // 得到 屏幕的宽度
    screenW = dm.widthPixels;
    screenH = dm.heightPixels;

    mInflater = LayoutInflater.from(mContext);
    options = new DisplayImageOptions.Builder()
        .showImageOnFail(R.drawable.default_banner).cacheInMemory(true) // 缓存
        .bitmapConfig(Bitmap.Config.RGB_565) // 图片格式
        .build();
    ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(
        mContext).writeDebugLogs().build();
    ImageLoader.getInstance().init(configuration);
  }

  public void updateData(ArrayList<PhotoInfoAlbum> list) {
    mList = list;
  }

  public void setEditor(boolean iseditor) {
    this.isEditor = iseditor;
  }

  public void setSelectAll(boolean isSelectAll) {
    this.isSelectAll = isSelectAll;
    isEditor = true;
  }

  @Override
  public int getCount() {
    return mList.size();
  }

  public boolean isEditor() {
    return isEditor;
  }

  @Override
  public Object getItem(int position) {
    return mList.get(position);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  public interface ButtonClickListener {
    void onEditClicked(int id, int pos);

    void onShareClicked(int id);
  }

  public void setButtonClickListener(ButtonClickListener mButtonClickListener) {
    this.mButtonClickListener = mButtonClickListener;
  }

  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
    final PhotoInfoAlbum item = mList.get(position);
    final ViewHolder holder;
    convertView = saArray.get(position);
    if (convertView == null) {
      convertView = mInflater.inflate(R.layout.photoclouditem, null);
      holder = new ViewHolder();
      holder.cloudphotogrid = (LinearLayout) convertView
          .findViewById(R.id.cloudphotogrid);
      holder.layout_task = convertView.findViewById(R.id.layout_task);
      holder.tv_date = (TextView) convertView.findViewById(R.id.tv_date);
      holder.tv_day = (TextView) convertView.findViewById(R.id.tv_day);
      holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
      holder.img_edit = (ImageView) convertView.findViewById(R.id.img_edit);
      holder.img_share = (ImageView) convertView.findViewById(R.id.img_share);

      convertView.setTag(holder);
      saArray.put(position, convertView);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    holder.img_edit.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (mButtonClickListener != null) {
          mButtonClickListener.onEditClicked(item.id, position);
        }
      }
    });

    holder.img_share.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (mButtonClickListener != null) {
          mButtonClickListener.onShareClicked(item.id);
        }
      }
    });

    if (Tools.isNotEmpty(item.title)) {
      holder.tv_content.setText(item.title);
      holder.tv_content.setVisibility(View.VISIBLE);
    }

    TextView tv_count;

    if (Tools.isNotEmpty(item.photo_infos) && !item.photo_infos.isEmpty()) {
      final ArrayList<ImageItem> aciList = item.photo_infos;
      View v = null;
      ImageView img_photo, img_photo2, img_photo3, img_photo4, img_photo5;

      screenW = dm.widthPixels;
      screenW = screenW - 2 * Tools.getPixelByDip(mContext, 8);

      switch (aciList.size()) {
      case 1:
        v = mInflater.inflate(R.layout.element_item_one, parent, false);
        img_photo = (ImageView) v.findViewById(R.id.img_photo);

        ImageLoader.getInstance().displayImage(aciList.get(0).url, img_photo,
            options);

        LayoutParams para;
        para = img_photo.getLayoutParams();
        para.height = screenW;
        para.width = screenW;
        img_photo.setLayoutParams(para);

        break;
      case 2:
        v = mInflater.inflate(R.layout.element_item_two, parent, false);
        img_photo = (ImageView) v.findViewById(R.id.img_photo);
        img_photo2 = (ImageView) v.findViewById(R.id.img_photo2);

        ImageLoader.getInstance().displayImage(aciList.get(0).url, img_photo,
            options);
        ImageLoader.getInstance().displayImage(aciList.get(1).url, img_photo2,
            options);

        LayoutParams para1;

        para1 = img_photo.getLayoutParams();
        para1.height = screenW / 2;
        para1.width = screenW / 2;
        img_photo.setLayoutParams(para1);

        LayoutParams para2;
        para2 = img_photo2.getLayoutParams();
        para2.height = screenW / 2;
        para2.width = screenW / 2;
        img_photo2.setLayoutParams(para2);

        break;
      case 3:
        v = mInflater.inflate(R.layout.element_item_three, parent, false);
        img_photo = (ImageView) v.findViewById(R.id.img_photo);
        img_photo2 = (ImageView) v.findViewById(R.id.img_photo2);
        img_photo3 = (ImageView) v.findViewById(R.id.img_photo3);

        ImageLoader.getInstance().displayImage(aciList.get(0).url, img_photo,
            options);
        ImageLoader.getInstance().displayImage(aciList.get(1).url, img_photo2,
            options);
        ImageLoader.getInstance().displayImage(aciList.get(2).url, img_photo3,
            options);

        LayoutParams para11;
        para11 = img_photo.getLayoutParams();
        para11.height = screenW;
        para11.width = screenW;
        img_photo.setLayoutParams(para11);

        LayoutParams para22;
        para22 = img_photo2.getLayoutParams();
        para22.height = screenW / 2;
        para22.width = screenW / 2;
        img_photo2.setLayoutParams(para22);

        LayoutParams para33;
        para33 = img_photo3.getLayoutParams();
        para33.height = screenW / 2;
        para33.width = screenW / 2;
        img_photo3.setLayoutParams(para33);

        // img_photo2 = (ImageView) v.findViewById(R.id.img_photo2);
        break;
      case 4:
        v = mInflater.inflate(R.layout.element_item_four, parent, false);
        img_photo = (ImageView) v.findViewById(R.id.img_photo);
        img_photo2 = (ImageView) v.findViewById(R.id.img_photo2);
        img_photo3 = (ImageView) v.findViewById(R.id.img_photo3);
        img_photo4 = (ImageView) v.findViewById(R.id.img_photo4);

        ImageLoader.getInstance().displayImage(aciList.get(0).url, img_photo,
            options);
        ImageLoader.getInstance().displayImage(aciList.get(1).url, img_photo2,
            options);
        ImageLoader.getInstance().displayImage(aciList.get(2).url, img_photo3,
            options);
        ImageLoader.getInstance().displayImage(aciList.get(3).url, img_photo4,
            options);

        LayoutParams para111;
        para111 = img_photo.getLayoutParams();
        para111.height = screenW / 2;
        para111.width = screenW / 2;
        img_photo.setLayoutParams(para111);

        LayoutParams para222;
        para222 = img_photo2.getLayoutParams();
        para222.height = screenW / 2;
        para222.width = screenW / 2;
        img_photo2.setLayoutParams(para222);

        LayoutParams para333;
        para333 = img_photo3.getLayoutParams();
        para333.height = screenW / 2;
        para333.width = screenW / 2;
        img_photo3.setLayoutParams(para333);

        LayoutParams para444;
        para444 = img_photo4.getLayoutParams();
        para444.height = screenW / 2;
        para444.width = screenW / 2;
        img_photo4.setLayoutParams(para444);

        break;
      case 5:
        v = mInflater.inflate(R.layout.element_item_five, parent, false);

        img_photo = (ImageView) v.findViewById(R.id.img_photo);
        img_photo2 = (ImageView) v.findViewById(R.id.img_photo2);
        img_photo3 = (ImageView) v.findViewById(R.id.img_photo3);
        img_photo4 = (ImageView) v.findViewById(R.id.img_photo4);
        img_photo5 = (ImageView) v.findViewById(R.id.img_photo5);

        ImageLoader.getInstance().displayImage(aciList.get(0).url, img_photo,
            options);
        ImageLoader.getInstance().displayImage(aciList.get(1).url, img_photo2,
            options);
        ImageLoader.getInstance().displayImage(aciList.get(2).url, img_photo3,
            options);
        ImageLoader.getInstance().displayImage(aciList.get(3).url, img_photo4,
            options);
        ImageLoader.getInstance().displayImage(aciList.get(4).url, img_photo5,
            options);

        LayoutParams para1111;
        para1111 = img_photo.getLayoutParams();
        para1111.height = screenW / 2;
        para1111.width = screenW / 2;
        img_photo.setLayoutParams(para1111);

        LayoutParams para2222;
        para2222 = img_photo2.getLayoutParams();
        para2222.height = screenW / 2;
        para2222.width = screenW / 2;
        img_photo2.setLayoutParams(para2222);

        LayoutParams para3333;
        para3333 = img_photo3.getLayoutParams();
        para3333.height = screenW / 3;
        para3333.width = screenW / 3;
        img_photo3.setLayoutParams(para3333);

        LayoutParams para4444;
        para4444 = img_photo4.getLayoutParams();
        para4444.height = screenW / 3;
        para4444.width = screenW / 3;
        img_photo4.setLayoutParams(para4444);

        LayoutParams para5555;
        para5555 = img_photo5.getLayoutParams();
        para5555.height = screenW / 3;
        para5555.width = screenW / 3;
        img_photo5.setLayoutParams(para5555);

        break;
      default:
        v = mInflater.inflate(R.layout.element_item_five, parent, false);

        img_photo = (ImageView) v.findViewById(R.id.img_photo);
        img_photo2 = (ImageView) v.findViewById(R.id.img_photo2);
        img_photo3 = (ImageView) v.findViewById(R.id.img_photo3);
        img_photo4 = (ImageView) v.findViewById(R.id.img_photo4);
        img_photo5 = (ImageView) v.findViewById(R.id.img_photo5);

        ImageLoader.getInstance().displayImage(aciList.get(0).url, img_photo,
            options);
        ImageLoader.getInstance().displayImage(aciList.get(1).url, img_photo2,
            options);
        ImageLoader.getInstance().displayImage(aciList.get(2).url, img_photo3,
            options);
        ImageLoader.getInstance().displayImage(aciList.get(3).url, img_photo4,
            options);
        ImageLoader.getInstance().displayImage(aciList.get(4).url, img_photo5,
            options);

        LayoutParams para11111;
        para11111 = img_photo.getLayoutParams();
        para11111.height = screenW / 2;
        para11111.width = screenW / 2;
        img_photo.setLayoutParams(para11111);

        LayoutParams para22222;
        para22222 = img_photo2.getLayoutParams();
        para22222.height = screenW / 2;
        para22222.width = screenW / 2;
        img_photo2.setLayoutParams(para22222);

        LayoutParams para33333;
        para33333 = img_photo3.getLayoutParams();
        para33333.height = screenW / 3;
        para33333.width = screenW / 3;
        img_photo3.setLayoutParams(para33333);

        LayoutParams para44444;
        para44444 = img_photo4.getLayoutParams();
        para44444.height = screenW / 3;
        para44444.width = screenW / 3;
        img_photo4.setLayoutParams(para44444);

        LayoutParams para55555;
        para55555 = img_photo5.getLayoutParams();
        para55555.height = screenW / 3;
        para55555.width = screenW / 3;
        img_photo5.setLayoutParams(para55555);

        tv_count = (TextView) v.findViewById(R.id.tv_count);
        tv_count.setText("共" + aciList.size() + "张照片");
        tv_count.setVisibility(View.VISIBLE);
        img_photo5.setColorFilter(new ColorMatrixColorFilter(BT_SELECTED));

        break;
      }

      holder.cloudphotogrid.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          Intent intent = new Intent();
          intent.setClass(mContext, WallPicActivity.class);
          intent.putExtra("imagelist", aciList);
          mContext.startActivityForResult(intent, 101);

        }
      });

      holder.cloudphotogrid.removeAllViews();
      holder.cloudphotogrid.addView(v);

      // SGVAdapter agva = new SGVAdapter(mContext, aciList, position, item.id);
      // // agva.setEdit(isEditMode);
      // holder.cloudphotogrid.setColumnCount(6);
      // holder.cloudphotogrid.setAdapter(agva);
      //
      // agva.notifyDataSetChanged();

      // holder.cloudphotogrid.setClickable(false);
      // holder.cloudphotogrid.setPressed(false);
      // holder.cloudphotogrid.setEnabled(false);

      int idx = item.shoot.indexOf(" ");
      String[] str = item.shoot.substring(0, idx).split("-");
      String date = str[0] + "." + str[1];

      holder.tv_day.setText(str[2]);
      holder.tv_date.setText(date);
    }

    return convertView;
  }

  // private final class SGVAdapter extends BaseAdapter {
  //
  // LayoutInflater mInflater;
  // ArrayList<ImageItem> mList;
  //
  // public SGVAdapter(Context ctx, ArrayList<ImageItem> list, int selPos,
  // int albumId) {
  //
  // mList = list;
  // mInflater = LayoutInflater.from(ctx);
  // }
  //
  // @Override
  // public int getCount() {
  // if (mList.size() > 5) {
  // return 5;
  // } else {
  // return mList.size();
  // }
  // }
  //
  // @Override
  // public Object getItem(int position) {
  // return null;
  // }
  //
  // @Override
  // public long getItemId(int position) {
  // return 0;
  // }
  //
  // // Random r = new Random();
  //
  // @Override
  // public View getView(final int position, View convertView, ViewGroup parent)
  // {
  // LayoutParams lp = null;
  // View v = null;
  //
  // int total = mList.size();
  //
  // if (total < 3) {
  // v = mInflater.inflate(R.layout.element_item2, parent, false);
  // } else {
  // v = mInflater.inflate(R.layout.element_item_one, parent, false);
  // }
  // lp = new LayoutParams(v.getLayoutParams());
  //
  // switch (position) {
  // case 0:
  // if (total == 1 || total == 3) {
  // lp.span = 6;
  // } else {
  // lp.span = 3;
  // }
  // break;
  // case 1:
  // if (total == 2 || total == 3 || total == 4 || total == 5) {
  // lp.span = 3;
  // } else {
  // lp.span = 3;
  // }
  // break;
  // case 2:
  // if (total == 4 || total == 3) {
  // lp.span = 3;
  // } else if (total >= 5) {
  // lp.span = 2;
  // }
  // break;
  // case 3:
  // if (total == 4) {
  // lp.span = 3;
  // } else if (total >= 5) {
  // lp.span = 2;
  // }
  // break;
  // case 4:
  // if (total >= 5) {
  // lp.span = 2;
  // }
  // break;
  // }
  //
  // ImageView img_photo;
  // TextView tv_count;
  //
  // img_photo = (ImageView) v.findViewById(R.id.img_photo);
  // tv_count = (TextView) v.findViewById(R.id.tv_count);
  //
  // if (mList.size() > 5 && position == 4) {
  // tv_count.setText("共" + mList.size() + "张照片");
  // tv_count.setVisibility(View.VISIBLE);
  // img_photo.setColorFilter(new ColorMatrixColorFilter(BT_SELECTED));
  // } else {
  // tv_count.setVisibility(View.GONE);
  // }
  //
  // img_photo.setOnClickListener(new View.OnClickListener() {
  // @Override
  // public void onClick(View v) {
  // Intent intent = new Intent();
  // intent.setClass(mContext, WallPicActivity.class);
  // intent.putExtra("imagelist", mList);
  // mContext.startActivityForResult(intent, 101);
  // // Intent intent = new Intent();
  // // intent.putExtra("link",
  // // "http://www.xiaoxiaoban.com.cn/shareAlbum/shareAlbum.html?pid="
  // // + mList.get(position).photo_cloud_id);
  // // intent.putExtra("shouldshare", true);
  // // intent.putExtra("photo_cloud_id",
  // // mList.get(position).photo_cloud_id);
  // // intent.setClass(mContext, SliderActivity.class);
  // // mContext.startActivity(intent);
  // }
  // });
  //
  // ImageLoader.getInstance().displayImage(mList.get(position).url,
  // img_photo, options);
  // v.setLayoutParams(lp);
  // return v;
  // }
  // }

  private static class ViewHolder {
    LinearLayout cloudphotogrid;
    TextView tv_date, tv_day, tv_content;
    ImageView img_edit, img_share;
    View layout_task;
  }

}
